package persistence;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;

import service.Authentication;
import service.EditBrokerController;

import com.puppycrawl.tools.checkstyle.gui.Main;

import custom_types.Afm;
import custom_types.Date;
import custom_types.EmailAddress;
import custom_types.Property;
import entities.Advertisement;
import entities.Broker;
import entities.JPAUtil;
import entities.User;

public class DAOTest {
	private EntityManager entityManager = JPAUtil.getCurrentEntityManager();

	public void prepareData() {
		Property pr = new Property(5, 2008, 5000, 32);
//		Advertisement ad = new Advertisement("testing", pr);
//		Advertisement ad2 = new Advertisement("testing2", pr);

		entityManager.getTransaction().begin();
//		entityManager.persist(ad);
//		entityManager.persist(ad2);
		entityManager.getTransaction().commit();

		entityManager.close();
	}
	
	@Test
	public void LoginTest(){
		entityManager = JPAUtil.getCurrentEntityManager();
		entityManager.getTransaction().begin();
		Broker broker = new Broker("mitsos","123",new EmailAddress("mits@mits.com"), "Demetris", "Procopiou", new Date(1,1,1), new Afm(12321));
		entityManager.persist(broker);
		entityManager.getTransaction().commit();
		entityManager.close();
		
		User user = Authentication.Login("mitsos");
	}
	
	@Ignore
	@Test
	public void test(){
		entityManager = JPAUtil.getCurrentEntityManager();
		Broker broker = new Broker("mitsos","123",new EmailAddress("mits@mits.com"), "Demetris", "Procopiou", new Date(1,1,1), new Afm(12321));
		//Broker broker1 = new Broker("mitsos1","123",new EmailAddress("mits@mits.com"), "Demetris", "Procopiou", new Date(1,1,1), new Afm(12321));
		Property pr = new Property(1,2,3,4);
		Advertisement ad = new Advertisement(broker,pr,new Date(1,1,1),new Date(1,1,1),true);
		broker.addAdvertisement(ad);
		
		
		
		Advertisement ad1 = new Advertisement(broker,pr,new Date(1,1,1),new Date(1,1,1),false);
		broker.addAdvertisement(ad1);
		entityManager.getTransaction().begin();
		entityManager.persist(broker);
		entityManager.persist(ad);
		entityManager.persist(ad1);
		
		
		entityManager.getTransaction().commit();
	//	broker = entityManager.find(Broker.class, 1);
	//	ad = entityManager.find(Advertisement.class, 1);
	//	System.out.println();
		System.out.println(ad.getBroker().getName());
		broker = entityManager.find(Broker.class,1 );
		System.out.println(broker.getAllAdvertisements().size());
		System.out.println(broker.getAdvertisement(1).getIsAvailable());

	//	System.out.println(broker.getName());
				
		entityManager.close();
		EditBrokerController ebcp = new EditBrokerController();
		ebcp.editBrokerProfile("mitsos");
	}
	
	@Ignore
	@Test
	public void addBrokerToDatabase(){
		entityManager = JPAUtil.getCurrentEntityManager();
		Broker broker = new Broker("mitsos","123",new EmailAddress("mits@mits.com"), "Demetris", "Procopiou", new Date(1,1,1), new Afm(12321));
		Broker broker1 = new Broker("mitsos1","123",new EmailAddress("mits@mits.com"), "Demetris", "Procopiou", new Date(1,1,1), new Afm(12321));
		User u = new User("mitsos-user","123",new EmailAddress("mits@mits.com"), "Demetris", "Procopiou", new Date(1,1,1));
		
		entityManager.getTransaction().begin();
		entityManager.persist(broker);
		entityManager.persist(broker1);
		entityManager.persist(u);
		entityManager.getTransaction().commit();
		//User u = entityManager.find(User.class, 1);
		
		broker = entityManager.find(Broker.class, 1);
		System.out.println(broker.getUsername() + " " + broker.getEmail());
		
		entityManager.getTransaction().begin();
		entityManager.remove(broker1);
		entityManager.getTransaction().commit();
		
		
		
		List<Broker> allBrokers = findAllBrokers();
		List<User> allUsers = findAllUsers();
		
		System.out.println("Brokers found: " + allBrokers.size());
		System.out.println("Users found: " + allUsers.size());
		
		entityManager.close();
	
	}
	
	@Ignore
	@Test
	public void testIfInserted() {
		
		prepareData();

		entityManager = JPAUtil.getCurrentEntityManager();
		Advertisement ad = entityManager.find(Advertisement.class, 1);

		entityManager.getTransaction().begin();
		entityManager.remove(ad);
		entityManager.getTransaction().commit();

		// entityManager.remove(ad);
		// tx.commit();
		Advertisement ad2 = entityManager.find(Advertisement.class, 2);
		//System.out.println(ad2.getName());
		List<Advertisement> allAds = findAllAdvertisements();
		System.out.println(allAds.size());
		// System.out.println(ad.getName());
		// System.out.println(ad2.getName());
		// ad2 = entityManager.find(Advertisement.class, 2);
		entityManager.close();
	}

	@SuppressWarnings("unchecked")
	private List<Advertisement> findAllAdvertisements() {
		List<Advertisement> allAdvertisements = entityManager.createQuery(
				"select b from Advertisement b").getResultList();
		return allAdvertisements;
	}
	
	@SuppressWarnings("unchecked")
	private List<Broker> findAllBrokers() {
		List<Broker> allBrokers = entityManager.createQuery(
				"select b from Broker b").getResultList();
		return allBrokers;
	}
	
	@SuppressWarnings("unchecked")
	private List<User> findAllUsers() {
		List<User> allUsers = entityManager.createQuery(
				"select b from User b").getResultList();
		return allUsers;
	}
	// public void findExistingBorrowerJpa() {
	// setUpJpa();
	// Borrower borrower = entityManager.find(Borrower.class,2);
	// Assert.assertEquals("����������", borrower.getFirstName());
	// }
	//
	//
	//
	//
	//
	// @Test
	// public void findNonExistingBorrowerJpa() {
	// setUpJpa();
	// Borrower borrower = entityManager.find(Borrower.class,4711);
	// Assert.assertNull(borrower);
	// }
	//
	//
	//
	// @Test
	// public void listAllBorrowersJpa() {
	// setUpJpa();
	// List<Borrower> allBorrowers = findAllBorrowers();
	// Assert.assertEquals(INITIAL_BORROWER_COUNT, allBorrowers.size());
	//
	// }
	//
	//
	//
	//
	//
	//
	//
	// @Test
	// public void saveBorrowerJpa() {
	// setUpJpa();
	// Borrower borrower = new Borrower(5000, "Giannis", "Martinopoulos", null,
	// null, null);
	// EntityTransaction tx = entityManager.getTransaction();
	// tx.begin();
	// entityManager.persist(borrower);
	// tx.commit();
	//
	// Assert.assertEquals(INITIAL_BORROWER_COUNT + 1,
	// findAllBorrowers().size());
	// Assert.assertNotNull(entityManager.find(Borrower.class,borrower.getBorrowerNo()));
	// Assert.assertTrue(findAllBorrowers().contains(borrower));
	// }
	//
	//
	//
	//
	//
	// @Test
	// public void deleteBorrowerJpa() {
	// setUpJpa();
	// List<Borrower> allBorrowers = findAllBorrowers();
	// Borrower borrower = allBorrowers.get(0);
	// EntityTransaction tx = entityManager.getTransaction();
	// tx.begin();
	// entityManager.remove(borrower);
	// tx.commit();
	//
	// allBorrowers = findAllBorrowers();
	//
	// Assert.assertEquals(INITIAL_BORROWER_COUNT - 1, allBorrowers.size());
	//
	// }
	//
	//
	//
	// /**
	// * ������������������ ������������������ ������ �������������� ������
	// �������� ������������������
	// */
	// @Test
	// public void findExistingItemJpa() {
	// setUpJpa();
	// String EXPECTED_ISBN_FROM_ITEM = "1";
	//
	// Item item = entityManager.find(Item.class, 1);
	// Assert.assertEquals(EXPECTED_ISBN_FROM_ITEM ,
	// item.getBook().getIsbn().getValue());
	// }
	//
	//
	//
	//
	// /**
	// * ������������������ ������������������ ������ ������ ��������������
	// ������ �������� ������������������
	// */
	// @Test
	// public void findNonExistingItemJpa() {
	// setUpJpa();
	// Item item = entityManager.find(Item.class, 4711);
	// Assert.assertNull(item);
	//
	// }
	//
	//
	// /**
	// * ������������������ �������� ������ ������������������
	// */
	// @Test
	// public void listAllItemsItemJpa() {
	// setUpJpa();
	// List<Item> allItems = finAllItems();
	// Assert.assertEquals(INITIAL_ITEM_COUNT, allItems.size());
	// }
	//
	//
	//
	//
	// /**
	// * �������������������� ������������������
	// */
	// @Test
	// public void saveItemJpa() {
	// setUpJpa();
	// Book book = new Book("One Title", new ISBN("9999"), null, 0, null);
	// Item item = new Item(10);
	// item.setBook(book);
	//
	// EntityTransaction tx = entityManager.getTransaction();
	// tx.begin();
	// entityManager.persist(item);
	// tx.commit();
	//
	// List<Item> allItems = finAllItems();
	// Assert.assertEquals(INITIAL_ITEM_COUNT + 1, allItems.size());
	// Assert.assertNotNull(entityManager.find(Item.class,
	// item.getItemNumber()));
	// Assert.assertTrue(allItems.contains(item));
	// }
	//
	//
	// /**
	// * ���������������� ������������������
	// */
	// @Test
	// public void deleteItemJpa() {
	// setUpJpa();
	// Item item = finAllItems().get(0);
	// item.setBook(null);
	// EntityTransaction tx = entityManager.getTransaction();
	// tx.begin();
	// entityManager.remove(item);
	// tx.commit();
	//
	// List<Item> allItems = finAllItems();
	// Assert.assertEquals(INITIAL_ITEM_COUNT - 1, allItems.size());
	// Assert.assertNull(entityManager.find(Item.class, item.getItemNumber()));
	// Assert.assertFalse(allItems.contains(item));
	// }
	//
	//
	//
	//
	//
	//
	//
	// /**
	// * �������������������� ������������������
	// */
	// @Test
	// public void saveLoanJpa() {
	// setUpJpa();
	// Loan loan = CreateNewLoan();
	// EntityTransaction tx = entityManager.getTransaction();
	// tx.begin();
	// entityManager.persist(loan);
	// tx.commit();
	// List<Loan> allLoans = findAllLoans();
	// Assert.assertEquals(INITIAL_LOAN_COUNT + 1, allLoans.size());
	//
	// }
	//
	//
	//
	//
	//
	//
	//
	//
	// private Loan CreateNewLoan() {
	// Borrower borrower = entityManager.find(Borrower.class,
	// BORROWER_NO_FOR_NEW_LOAN);
	// Item item = entityManager.find(Item.class, ITEM_NO_FOR_NEW_LOAN);
	// Loan loan = item.borrow(borrower);
	// return loan;
	// }
	//

	// @SuppressWarnings("unchecked")
	// private List<Borrower> findAllBorrowers() {
	// List<Borrower> allBorrowers = entityManager
	// .createQuery("select b from Borrower b")
	// .getResultList();
	// return allBorrowers;
	// }
	//
	// @SuppressWarnings("unchecked")
	// private List<Item> finAllItems() {
	// List<Item> allItems = entityManager
	// .createQuery("select i from Item i")
	// .getResultList();
	// return allItems;
	// }
	//
	// @SuppressWarnings("unchecked")
	// private List<Loan> findAllLoans() {
	// List<Loan> allLoans = entityManager
	// .createQuery("select l from Loan l")
	// .getResultList();
	// return allLoans;
	// }
}
